;(function(){

     var share = {};


     share.dataLabelNum = 30;

     share.getDataType = function(time,tempOpt) {
            if (tempOpt.group == 'day') {
                time = time;
            } else if (tempOpt.group == 'month') {
                var arr = time.match(/-{1}(.*)\(.*/);
                time = arr[1];
            } else if (tempOpt.group == 'week') {
                time = time;
            }
            return time;
      };


     share.getGraphHeight = function(opt) {
           var baseHeight = 400,height=400; 
           if (typeof(opt.group) !== 'undefined') {
               var time = 24*60*60*1000,num=30,
                   diff = new Date(opt.end_date).getTime() - new Date(opt.begin_date).getTime();
               if (opt.group == 'day') { time = 24*60*60*1000, num=30};
               if (opt.group == 'week') { time = 24*60*60*1000*7, num=25};
               if (opt.group == 'month') { time = 24*60*60*1000*30,num=25};
               var val = Math.ceil((diff/time)/num);
               height =  val * (val < 14 ? 20 : 15) + baseHeight;
           }
           return height;
     };
 
     share.ajaxControl = function(ajaxOpt,cache,key) {
           var obj = _.clone(ajaxOpt);
               obj.timeout = 120000;
               obj.error = function(xhr, errorType, error) {
                   alert(error+', http request failed!'); 
                   ajaxOpt.error(xhr,errorType,error);
               };
               obj.complete = function(XMLHttpRequest,status){
            　　　 //if(status == 'timeout'){}
                   ajaxOpt.complete(XMLHttpRequest,status);
            　　}
           /*
           if (cache) {
               obj.success = function(data) {
                   share.setSessionStorage(key,JSON.stringify(data)); 
                   ajaxOpt.success(data);    
               };
           }
           */
           return obj;
     };

     share.ajax = function(ajaxOpt,cache) {
           /*
           if (cache) { 
               var key = hex_md5(JSON.stringify(ajaxOpt.data) + ajaxOpt.url),
                   data = share.getSessionStorage(key);
               if (data) {
                   data = JSON.parse(data); 
                   ajaxOpt.success(data);
                   ajaxOpt.complete();
               } else {
                   $.ajax(this.ajaxControl(ajaxOpt,cache,key));
               }
           } else {
               $.ajax(this.ajaxControl(ajaxOpt,cache));
           }
           */
           $.ajax(this.ajaxControl(ajaxOpt,cache));
     };

     share.getSiteName = function(id) {
             var siteInfo = share.getLocalStorage("siteInfo");
                 siteInfo[0] = 'All sites';
             id = (id == '') ? id = 0 : id;
             return siteInfo[id].name || 'All sites'; 
     };

     share.load = function(dom,bool) {
         if (bool) {
             var html = "<div id='load'></div>";
             if ($('#load').length<1) {
                 dom.append(html);
             }
         } else {
            dom.find('#load').remove();
         }
         this.cover(dom,bool);
     };

     share.cover = function(dom,bool) {
         if (bool) {
             var html = "<div id='loadCover'></div>";
             if ($('#loadCover').length < 1) {
                 dom.append(html);
             }
         } else {
             dom.find('#loadCover').remove();
         }
     };

     share.changeInputDate = function(val) {
            switch(val) {
                case 'last-week' :
                    sdate = util.getCurrentDate(7); 
                    edate = util.getCurrentDate();  
                break;

                case 'last-month':
                    sdate = util.lastNumMonth(1); 
                    edate = util.getCurrentDate(); 
                break;

                case 'last-three-months':
                    sdate = util.lastNumMonth(3); 
                    edate = util.getCurrentDate(); 
                break; 

                case 'last-year' :
                    sdate = util.lastNumMonth(12); 
                    edate = util.getCurrentDate(); 
                break
             } 
             return {sdate:sdate,edate:edate}; 
     };

     share.setSessionStorage = function(key,val) {
          val =  _.isObject(val) ? JSON.stringify(val) : val;         
          try {
              sessionStorage.setItem(key,val);
          } catch(oException){
              if(oException.name == 'QuotaExceededError'){
                  //console.log('超出本地存储限额！');
                  sessionStorage.clear();
                  sessionStorage.setItem(key,val);
              }
          }
     };

     share.getSessionStorage = function(key) {
         return sessionStorage.getItem(key);
     };

     share.setLocalStorage = function(key,val) {
         val =  _.isObject(val) ? JSON.stringify(val) : val;         
         localStorage.setItem(key,val);
     };

     share.getLocalStorage = function(key) {
         return JSON.parse(localStorage.getItem(key));
     };


     share.setInputDate = function(val,mId) {
         var dom = $('#'+mId);
         if (val !== '') {
            var obj = share.changeInputDate(val);
            dom.find('.from_time').val(obj.sdate);
            dom.find('.to_time').val(obj.edate);
         } else {
            dom.find('.from_time').val('');
            dom.find('.to_time').val('');
         }
     };  
 
    share.getAllSites = function(a) {
        var siteInfo = share.getLocalStorage("siteInfo"), sitesInfo = [];
        if (a == 1) {
            siteInfo[0] = {name:'All sites',nameall:'All sites',index:-1,id:0};
        }
        for ( var i in siteInfo) {
            var obj = siteInfo[i];
            sitesInfo.push(obj);
        }
        sitesInfo.sort(function(a, b) { 
            return a.index - b.index;
        })
        return sitesInfo;
    };

     share.getSiteAllName = function(key) {
         var siteInfo = share.getLocalStorage("siteInfo");
         var obj = _.find(siteInfo,function(val){
             return (key == val.name);
         });
         return obj.allname;
     };

     share.getPostData = function(tempOpt,mId) {
             var dom = $('#'+mId);
             
             tempOpt.engine = dom.find('.selectInfo').find('select').val();
             if (tempOpt.engine == '0') { tempOpt.engine = ''};
             tempOpt.end_date = dom.find('.to_time').val();
             tempOpt.begin_date = dom.find('.from_time').val();
             tempOpt.group = dom.find('.selectGroup').val();           

             if (tempOpt.end_date == '' || tempOpt.begin_date == '') {
                 alert('The time is required!');
                 return false;
             }
             if (new Date(tempOpt.end_date) < new Date(tempOpt.begin_date)) {
                 alert('The end time is less than the start time!');
                 return false;
             }
             if (new Date(tempOpt.end_date) > new Date()) {
                 alert('The end time is greater than the current time!');
                 return false;
             }

             return tempOpt;
      }; 

      share.cacheSelect = function(val) {
            share.setLocalStorage('siteSelect',val);
      };

      share.setSite = function(dom) {
            var val = share.getLocalStorage('siteSelect');
            if (val !== null) {
                dom.find('.selectInfo').find('option[value="'+val+'"]').attr("selected",true);
            }
      };

     window.share = share;    
})();
